//
//  JDNewCustomTitleView.swift
//  DragonWallet
//
//  Created by volantPig on 2025/9/7.
//

import UIKit

class JDNewCustomTitleView: UIView {

    lazy var navigatiionBgImageV: UIImageView = {
        let navigationImage = UIImageView()
        navigationImage.image = UIImage(named: "navigationBar_bg_image")
        navigationImage.isUserInteractionEnabled = true
        return navigationImage
    }()
    
//    lazy var navigatiionBgImageV: UIView = {
//        let navigationImage = UIView()
//        navigationImage.backgroundColor = .orange
//        return navigationImage
//    }()
    
    
//    var myDelegate: CustomTitleViewDelegate?
//    var leftButton = UIButton(type: .custom)
//    var rightButton = UIButton(type: .custom)
//    var bottomLine = UIView()
    
    lazy var logoButton: UIButton = {
        let logo = UIButton(type: .custom)
        logo.setImage(UIImage(named: "logo_yellow"), for: .normal)
        
        return logo
    }()
    
    lazy var searchButton: UIButton = {
        let search = UIButton(type: .custom)
        search.setImage(UIImage(named: "search_white"), for: .normal)
        search.backgroundColor = color("#532408").withAlphaComponent(0.39)
        search.makeCorner(16)
        return search
    }()
    
    lazy var leftTitleLabel: UIButton = {
        let leftTitle = UIButton(type: .custom)
        leftTitle.setTitle("跨境直邮", for: .normal)
        leftTitle.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .regular)
        leftTitle.setTitleColor(color("#FFD6CC"), for: .normal)
        leftTitle.addTarget(self, action: #selector(leftButtonAction), for: .touchUpInside)
        return leftTitle
    }()

    lazy var rightTitleLabel: UIButton = {
        let rightTitle = UIButton(type: .custom)
        rightTitle.setTitle("境外预售", for: .normal)
        rightTitle.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .regular)
        rightTitle.setTitleColor(color("#FFD6CC"), for: .normal)
        rightTitle.addTarget(self, action: #selector(rightButtonAction), for: .touchUpInside)
        return rightTitle
    }()
    
    lazy var moveLine: UIImageView = {
        let line = UIImageView()
        line.image = UIImage(named: "home_nav_line")
        line.makeCorner(1)
        return line
    }()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUIElements()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUIElements() {
        
        self.addSubview(self.navigatiionBgImageV)
        navigatiionBgImageV.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.centerX.equalToSuperview()
        }
//        navigatiionBgImageV.isUserInteractionEnabled = false
        
        self.addSubview(self.logoButton)
        self.logoButton.snp.makeConstraints { make in
            make.leading.equalTo(self).offset(26)
            make.bottom.equalTo(self).offset(-24)
            make.size.equalTo(CGSizeMake(52, 52))
        }
        
        
        self.addSubview(self.searchButton)
        self.searchButton.snp.makeConstraints { make in
            make.centerY.equalTo(self.logoButton)
            make.trailing.equalTo(self).offset(-16)
            make.size.equalTo(CGSizeMake(32, 32))
        }

        self.addSubview(self.leftTitleLabel)
        self.leftTitleLabel.snp.makeConstraints { make in
            make.trailing.equalTo(self.snp.centerX).offset(-14)
            make.centerY.equalTo(self.logoButton)
            make.height.equalTo(28)
        }
        
        self.addSubview(self.rightTitleLabel)
        self.rightTitleLabel.snp.makeConstraints { make in
            make.leading.equalTo(self.snp.centerX).offset(14)
            make.centerY.equalTo(self.logoButton)
            make.height.equalTo(28)
        }
//        self.rightTitleLabel.backgroundColor = .green
        
        self.addSubview(self.moveLine)
        self.moveLine.snp.makeConstraints { make in
            make.centerX.equalTo(leftTitleLabel)
            make.size.equalTo(CGSizeMake(25, 2))
            make.top.equalTo(self.leftTitleLabel.snp.bottom).offset(4)
        }
        
        
        
        
//        self.addSubview(self.leftButton)
//        self.leftButton.frame = CGRect(x: 0, y: 0, width: 67, height: 44)
//        
//        self.leftButton.setTitle("跨境直邮", for: .normal)
//        self.leftButton.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
//        self.leftButton.setTitleColor(color("#333333"), for: .normal)
//        self.leftButton.addTarget(self, action: #selector(leftButtonAction), for: .touchUpInside)
//        
//        self.addSubview(self.rightButton)
//        self.rightButton.frame = CGRect(x: 110, y: 0, width: 67, height: 44)
//        self.rightButton.setTitle("境外预售", for: .normal)
//        self.rightButton.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
//        self.rightButton.setTitleColor(color("#333333"), for: .normal)
//        self.rightButton.addTarget(self, action: #selector(rightButtonAction), for: .touchUpInside)
//        
//        self.addSubview(self.bottomLine)
////        177 30
//        self.bottomLine.frame = CGRect(x: 21, y: 41, width: 25, height: 3)
//        self.bottomLine.backgroundColor = color("#FA6161")
    }
    
    @objc func leftButtonAction() {
        print("左边")
        self.leftTitleLabel.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .semibold)
        self.leftTitleLabel.setTitleColor(color("#FFF4C5"), for: .normal)
        self.rightTitleLabel.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .regular)
        self.rightTitleLabel.setTitleColor(color("#FFD6CC"), for: .normal)
//        self.bottomLine.frame = CGRect(x: 21, y: 41, width: 25, height: 3)
////        self.myDelegate?.customTitleDidSelected(at: 0)
        UIView.animate(withDuration: 0.5) {
            self.moveLine.snp.remakeConstraints { make in
                make.centerX.equalTo(self.leftTitleLabel)
                make.size.equalTo(CGSizeMake(25, 2))
                make.top.equalTo(self.leftTitleLabel.snp.bottom).offset(4)
            }
            self.moveLine.layoutIfNeeded()
            self.layoutIfNeeded()
        }
    }
    
    @objc func rightButtonAction() {
        print("右边")
        self.leftTitleLabel.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .regular)
        self.leftTitleLabel.setTitleColor(color("#FFD6CC"), for: .normal)
        self.rightTitleLabel.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .semibold)
        self.rightTitleLabel.setTitleColor(color("#FFF4C5"), for: .normal)
//        self.bottomLine.frame = CGRect(x: 131, y: 41, width: 25, height: 3)
////        self.myDelegate?.customTitleDidSelected(at: 1)
            
        
        UIView.animate(withDuration: 0.5) {
            self.moveLine.snp.remakeConstraints { make in
                make.centerX.equalTo(self.rightTitleLabel)
                make.size.equalTo(CGSizeMake(25, 2))
                make.top.equalTo(self.leftTitleLabel.snp.bottom).offset(4)
            }
            self.moveLine.layoutIfNeeded()
            self.layoutIfNeeded()
        }
        
    }
    
    

}
